﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


using CRM.Entity;
using CRM.BLL;
using DP.Web.UI;
using DP.Common;
using DP.Web.UI.Controls;

namespace CRM.Web.FormManage
{
    public partial class FormFieldModify : Common.PageBase
    {

        T_FormFieldInfo_BLL bll = new T_FormFieldInfo_BLL();
        T_FormInfo_BLL fiBll = new T_FormInfo_BLL();
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            PageHelper.SetPrePageUrl(this.Page);
            PageHelper.SetGoBackUrl(this.Page, btnCancel);
            PageHelper.SetPopCalender(this.Page, txtCreateTime);
            PageHelper.SetPopCalender(this.Page, txtModifyTime);

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlFormIDBinding();
                ddlIsEnabledBinding();
                ddlIsPostBackBinding();
                ddlIsBlankRowBinding();
                ddlExclusiveRowBinding();
                ddlFieldTypeBinding();
                ddlDataSourceTypeBinding();
                ddlIsVisibleBinding();
                ddlIsPrimaryKeyBinding();



                pageDataInit();
            }
        }

        #region 页面初始化数据绑定

        /// <summary>
        ///	表单编号 下拉列表数据绑定
        /// </summary>
        private void ddlFormIDBinding()
        {
            ddlFormID.DataSource = fiBll.GetList();
            ddlFormID.DataTextField = "TableDescription";
            ddlFormID.DataValueField = "FormID";
            ddlFormID.DataBind();
            ddlFormID.Items.Insert(0, new ListItem("", ""));

        }
        private void ddlIsEnabledBinding()
        {
            EnumHelper.FillListControl(ddlIsEnabled, typeof(IsEnabled));
            //ddlIsEnabled.Items.Add(new ListItem("", ""));
            //ddlIsEnabled.DataSource = fiBll.GetList();
            //ddlIsEnabled.DataTextField = "TableDescription";
            //ddlIsEnabled.DataValueField = "FormID";
            //ddlIsEnabled.DataBind();
            //ddlIsEnabled.Items.Insert(0, new ListItem("", ""));
        }
        private void ddlIsPostBackBinding()
        {
            EnumHelper.FillListControl(ddlIsPostBack, typeof(IsPostBack));
        }
        private void ddlIsBlankRowBinding()
        {
            EnumHelper.FillListControl(ddlIsBlankRow, typeof(IsBlankRow));
        }
        private void ddlExclusiveRowBinding()
        {

        }
        private void ddlFieldTypeBinding()
        {
            EnumHelper.FillListControl(ddlFieldType, typeof(WebFormFieldType));
        }
        private void ddlDataSourceTypeBinding()
        {
            EnumHelper.FillListControl(ddlDataSourceType, typeof(DataSourceType));
        }

        private void ddlIsVisibleBinding()
        {
            EnumHelper.FillListControl(ddlIsVisible, typeof(IsVisible));

        }

        private void ddlIsPrimaryKeyBinding()
        {

            EnumHelper.FillListControl(ddlIsPrimaryKey, typeof(IsPrimaryKey));
        }

        #endregion

        private void pageDataInit()
        {
            string _formfieldid = String.IsNullOrEmpty(Request.QueryString["FormFieldID"]) ? "" : Request.QueryString["FormFieldID"].Trim();

            if (!String.IsNullOrEmpty(_formfieldid))
            {
                T_FormFieldInfo_Entity entity = bll.GetEntity("FormFieldID", _formfieldid);
                if (entity != null)
                {
                    PageHelper.SetControlValue<T_FormFieldInfo_Entity>(this.Page, entity);
                }
            }

            txtFormFieldID.ReadOnly = true;

        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            string _formfieldid = String.IsNullOrEmpty(Request.QueryString["FormFieldID"]) ? "" : Request.QueryString["FormFieldID"].Trim();


            T_FormFieldInfo_Entity entity = bll.GetEntity("FormFieldID", _formfieldid);

            if (entity != null)
            {
                PageHelper.GetControlValue<T_FormFieldInfo_Entity>(this.Page, ref entity);
                entity.ModifyPeople = _userLoginInfo.UserEntity.USERNAME;
                entity.ModifyPeopleId = _userLoginInfo.UserEntity.USERID.ToString();
                entity.ModifyTime = DateTime.Now;


                try
                {
                    long rel = bll.Update(entity);
                    if (rel > 0)
                    {
                        PageHelper.ShowMessage(this.Page, "修改成功！", UserLoginInfo.FilterBackUrl(PageHelper.GetPrePageUrl(this), "FormFieldList.aspx"));
                    }
                    else
                    {
                        PageHelper.ShowMessage(this.Page, "修改失败！");
                    }
                }
                catch (Exception ex)
                {

                    PageHelper.ShowMessage(this.Page, "修改失败！" + ex.Message);
                }
            }
            else
            {
                entity = new T_FormFieldInfo_Entity();

                PageHelper.GetControlValue<T_FormFieldInfo_Entity>(this.Page, ref entity);
                entity.FormFieldID = Guid.NewGuid().ToString();
                entity.CreatePeople = _userLoginInfo.UserEntity.USERNAME;
                entity.CreatePeopleId = _userLoginInfo.UserEntity.USERID.ToString();
                entity.CreateTime = DateTime.Now;
                entity.ModifyPeople = _userLoginInfo.UserEntity.USERNAME;
                entity.ModifyPeopleId = _userLoginInfo.UserEntity.USERID.ToString();
                entity.ModifyTime = DateTime.Now;

                try
                {
                    long rel = bll.Insert(entity);
                    if (rel > 0)
                    {
                        PageHelper.ShowMessage(this.Page, "添加成功！", "FormFieldList.aspx");
                    }
                    else
                    {
                        PageHelper.ShowMessage(this.Page, "添加失改！");
                    }

                }
                catch (Exception ex)
                {

                    PageHelper.ShowMessage(this.Page, "添加失改！" + ex.Message);
                }
            }
        }
    }
}
